/**
 * @param {number[]} materials
 * @param {number[][]} cookbooks
 * @param {number[][]} attribute
 * @param {number} limit
 * @return {number}
 */
 var perfectMenu = function(materials, cookbooks, attribute, limit) {
  const n = cookbooks.length
  let ans = -1
  for(let i = 1; i<1<<n; i++) {
    let now = [...materials]
    let a = 0, b = 0
    let flag = true
    for(let j = 0; j<n && flag; j++) {
      if((i >> j) & 1) {
        for(let x = 0; x < cookbooks[j].length; x++) {
          if(now[x] < cookbooks[j][x]) {
            flag = false
            break
          }
          now[x] -= cookbooks[j][x]
        }
        a += attribute[j][0]
        b += attribute[j][1]
      }
    }
    if(flag && b >= limit) {
      ans = Math.max(ans, a)
    }
  }
  return ans
};